package com.gulimall.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;
import com.gulimall.product.dao.CategoryDao;
import com.gulimall.product.entity.CategoryEntity;
import com.gulimall.product.service.CategoryService;


/**
 * @author ht136
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Autowired
    CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> entities = baseMapper.selectList(null);
        return entities.stream().filter((item) -> item.getParentCid() == 0).map((item) -> {
            item.setChildren(getChildrens(item, entities));
            return item;
        }).collect(Collectors.toList());
    }

    @Override
    public Map<String,List<Object>> listWithTreeByCatId(Long catId) {
        List<CategoryEntity> nodeChain = getNodeChain(catId, baseMapper.selectList(null), new ArrayList<>());
        Map<String,List<Object>>listMap = new LinkedHashMap<>(16);
        List<Object> ids = new ArrayList<>();
        List<Object> names = new ArrayList<>();
        for (CategoryEntity categoryEntity : nodeChain) {
            ids.add(categoryEntity.getCatId());
            names.add(categoryEntity.getName());
        }
        listMap.put("ids",ids);
        listMap.put("names",names);
        return listMap;
    }

    @Override
    public void removeMenuByIds(List<Long> catIds) {
        //TODO 检查
        baseMapper.deleteBatchIds(catIds);
    }

    private List<CategoryEntity> getNodeChain(Long catId, List<CategoryEntity> allCategoryEntity, List<CategoryEntity> nodeChainList) {
        List<CategoryEntity> categoryEntities = allCategoryEntity.stream().filter(item ->
                item.getCatId().equals(catId)
        ).collect(Collectors.toList());
        if (categoryEntities.size() > 0 && categoryEntities.get(0).getCatLevel() > 0) {
            nodeChainList.add(categoryEntities.get(0));
            return getNodeChain(categoryEntities.get(0).getParentCid(), allCategoryEntity, nodeChainList);
        }
        return nodeChainList.stream().sorted(Comparator.comparingInt(CategoryEntity::getCatLevel)).collect(Collectors.toList());
    }

    private List<CategoryEntity> getChildrens(CategoryEntity categoryEntity, List<CategoryEntity> allCategoryEntity) {
        return allCategoryEntity.stream().filter(item ->
                item.getParentCid().equals(categoryEntity.getCatId())
        ).map((item) -> {
            item.setChildren(getChildrens(item, allCategoryEntity));
            return item;
        }).sorted((item1, item2) -> {
            Integer sort1 = item1.getSort();
            Integer sort2 = item2.getSort();
            return (sort1 == null ? 0 : sort1) - (sort2 == null ? 0 : sort2);
        }).collect(Collectors.toList());
    }
}